package com.xxxy.hyc.xyxmp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.xxxy.hyc.xyxmp.R;
import com.xxxy.hyc.xyxmp.adapter.CartAdapter;
import com.xxxy.hyc.xyxmp.bean.CartItem;
import com.xxxy.hyc.xyxmp.utils.CartManager;

import java.util.List;

public class CartActivity extends AppCompatActivity implements CartAdapter.OnCartItemChangeListener {

    private RecyclerView rvCart;
    private TextView tvTotalPrice;
    private Button btnSettle;
    private CartAdapter cartAdapter;
    private List<CartItem> cartItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_cart);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        
        // 显示返回按钮
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        initViews();
        initData();
        initRecyclerView();
        updateTotalPrice();
    }

    private void initViews() {
        rvCart = findViewById(R.id.rv_cart);
        tvTotalPrice = findViewById(R.id.tv_total_price);
        btnSettle = findViewById(R.id.btn_settle);
        
        btnSettle.setOnClickListener(v -> {
            if (cartItems.isEmpty()) {
                Toast.makeText(this, "购物车为空", Toast.LENGTH_SHORT).show();
            } else {
                // 跳转到订单确认页面
                Intent intent = new Intent(this, OrderActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initData() {
        cartItems = CartManager.getInstance().getCartItems();
    }

    private void initRecyclerView() {
        cartAdapter = new CartAdapter(this, cartItems);
        cartAdapter.setOnCartItemChangeListener(this);
        rvCart.setLayoutManager(new LinearLayoutManager(this));
        rvCart.setAdapter(cartAdapter);
    }

    private void updateTotalPrice() {
        double total = CartManager.getInstance().getTotalPrice();
        tvTotalPrice.setText("合计：¥" + String.format("%.2f", total));
    }

    @Override
    public void onQuantityChanged() {
        updateTotalPrice();
        cartAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemDeleted() {
        updateTotalPrice();
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}